package com.opener.web.spring.context;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;

/**
 * Created by baboy on 12/12/2016.
 */
public class AppContextListener extends ContextLoaderListener {
    private static ServletContext servletContext;
    @Override
    public void contextInitialized(ServletContextEvent event){
        super.contextInitialized(event);
        servletContext = event.getServletContext();
    }
    public static ApplicationContext getRootContext(){
        //获取web环境下的ApplicationContext
        ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        //将ApplicationContext，set到ContextUtil的静态变量context
        return ctx;
    }
}
